/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Welcome;

import ReadXML.ImageInfo;
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class q1 {

    private Image sample;
    private static ImageInfo imageInfo;
    private String as1, as2, as3, as4;
    private Canvas draw;
    public static Font f;
    public static int line = 1, aline = 1, trans, y, i;
    public static boolean isMenu, isAnswer, change = true;

    public q1(Canvas draw, ImageInfo imageInfo) {
        this.draw = draw;
        isMenu = false;

        if (this.imageInfo == null) {
            System.out.println("create0");
            this.imageInfo = imageInfo;
        }

        imageInfo = null;
        System.gc();
    }

    public void createQuestion(int time) {
        change = true;
        isAnswer = false;
        DrawTrialLevel.time = time;
        i = DrawTrialLevel.imgID;
        DrawTrialLevel.imgID++;

        Runtime rt = Runtime.getRuntime();
        System.out.println("q2bb = " + rt.freeMemory());
        String[][] asw = (String[][]) imageInfo.getVanswer(i);
        as1 = asw[0][0];
        as2 = asw[0][1];
        as3 = asw[0][2];
        as4 = asw[0][3];
        System.out.println("q2bb = " + rt.freeMemory());
        sample = PictureList.getImage(i);
        System.out.println("q2bb = " + rt.freeMemory());
        if (sample.getWidth() + 30 > draw.getWidth()) {
            System.out.println("change");
            sample = ImageScaled.CreateScaledImage(sample, draw.getWidth() - 30,
                    sample.getHeight() * (draw.getWidth() - 30) / draw.getWidth());
        }

        System.out.println("q2b = " + rt.freeMemory());
        asw = null;
        System.gc();
        System.out.println("q2a = " + rt.freeMemory());
    }

    private String longestString(String s1, String s2, String s3, String s4) {
        Font font = Font.getDefaultFont();
        String longest = null;
        String[] tempStr = new String[4];
        tempStr[0] = s1;
        tempStr[1] = s2;
        tempStr[2] = s3;
        tempStr[3] = s4;
        longest = tempStr[0];
        for (int i = 1; i < 4; i++) {
            if (font.stringWidth(tempStr[i]) > font.stringWidth(longest)) {
                longest = tempStr[i];
            }
        }
        tempStr = null;
        System.gc();
        return longest;
    }

    public int lengthString() {
        Font font = Font.getDefaultFont();
        return font.stringWidth(longestString(as1, as2, as3, as4));
    }

    public void render(Graphics g) {
        int time = DrawTrialLevel.time;

        f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);

        Font font = Font.getDefaultFont();
        g.setFont(font);
        g.setColor(0x00f6bd35);

        y = (draw.getHeight() - sample.getHeight()) / 2 - 5 - (font.stringWidth("60s") + 16);
        if (y < f.getHeight() + 4 && change) {
            trans = f.getHeight() + 4 - y;
        }

        g.fillRoundRect((draw.getWidth() - sample.getWidth()) / 2 - 10, (draw.getHeight() - sample.getHeight()) / 2 - 14 - font.stringWidth("60s") / 2 + trans,
                sample.getWidth() + 20, sample.getHeight() + 29 + font.stringWidth("60s") / 2, 5, 5);

        g.setColor(0xffffff);
        g.fillArc(draw.getWidth() / 2 - (font.stringWidth("60s") + 16) / 2,
                (draw.getHeight() - sample.getHeight()) / 2 - 5 - (font.stringWidth("60s") + 16) + trans,
                font.stringWidth("60s") + 16, font.stringWidth("60s") + 16, 0, 360);
        g.setColor(0x00b0711f);
        g.fillArc(draw.getWidth() / 2 - (font.stringWidth("60s") + 8) / 2,
                (draw.getHeight() - sample.getHeight()) / 2 - 9 - (font.stringWidth("60s") + 8) + trans,
                font.stringWidth("60s") + 8, font.stringWidth("60s") + 8, 0, 360);

        g.setColor(0xffffff);
        g.drawImage(sample, draw.getWidth() / 2, draw.getHeight() / 2 + trans, Graphics.HCENTER | Graphics.VCENTER);
        g.drawString(time + "s", draw.getWidth() / 2 + 1,
                (draw.getHeight() - sample.getHeight()) / 2 - 14 - font.stringWidth("60s") / 2 + font.getHeight() / 2 - 1 + trans,
                Graphics.HCENTER | Graphics.BASELINE);
        g.setColor(0x000000);
        g.drawString(PassTextboxText.getTextBoxText(), 5, draw.getHeight() / 2 + sample.getHeight() / 2 + 18 + trans,
                Graphics.TOP | Graphics.LEFT);

        g.setColor(0x00DD6F00);
        g.fillRect(0, draw.getHeight() - font.getHeight() - 2, draw.getWidth(), font.getHeight() + 2);
        g.setColor(0xffffff);
        g.setFont(f);

        g.setColor(0x00b0711f);
        g.fillRect(0, 0, draw.getWidth(), f.getHeight() + 4);
        g.fillRect(0, draw.getHeight() - f.getHeight() - 4, draw.getWidth(), f.getHeight() + 4);
        g.setColor(0xffffff);
        g.drawString("Đuổi hình bắt chữ", 2, 2, Graphics.LEFT | Graphics.TOP);

        if (!isMenu && !isAnswer) {
            g.drawString("Menu", 2, draw.getHeight() - 1, Graphics.LEFT | Graphics.BOTTOM);
            if (DrawChoice.level == 1) {
                g.drawString("Chọn đáp án", draw.getWidth() - 2, draw.getHeight() - 1, Graphics.RIGHT | Graphics.BOTTOM);
            } else {
                g.drawString("Nhập đáp án", draw.getWidth() - 2, draw.getHeight() - 1, Graphics.RIGHT | Graphics.BOTTOM);
            }
        } else {
            g.drawString("Chọn", 2, draw.getHeight() - 1, Graphics.LEFT | Graphics.BOTTOM);
            g.drawString("Trở lại", draw.getWidth() - 2, draw.getHeight() - 1, Graphics.RIGHT | Graphics.BOTTOM);
        }

        g.drawLine(0, draw.getHeight() - f.getHeight() - 3, draw.getWidth(), draw.getHeight() - f.getHeight() - 3);

        String s2 = "Bỏ qua";
        String s1 = "Trợ giúp";

        g.setFont(font);
        if (isMenu) {
            //draw Menu
            g.setColor(0x00000000);
            g.drawRect(0, draw.getHeight() - 3 * font.getHeight() - 10, font.stringWidth(s1) + 21, 2 * font.getHeight() + 8);
            g.setColor(0x00F0E001);
            g.fillRect(0 + 1, draw.getHeight() - 3 * font.getHeight() - 10 + 1, font.stringWidth(s1) + 20, 2 * font.getHeight() + 6);


            if (line == 1) {
                g.setColor(0x0041F001);
                g.fillRect(0 + 1, draw.getHeight() - 3 * font.getHeight() - 10 + 1, font.stringWidth(s1) + 20, font.getHeight() + 4);
            }
            if (line == 2) {
                g.setColor(0x0041F001);
                g.fillRect(0 + 1, draw.getHeight() - 2 * font.getHeight() - 6, font.stringWidth(s1) + 20, font.getHeight() + 3);
            }

            g.setColor(0x00000000);
            g.drawString(s2, 5, draw.getHeight() - font.getHeight() - 4 - 2, Graphics.LEFT | Graphics.BASELINE);
            g.drawString(s1, 5, draw.getHeight() - 2 * font.getHeight() - 4 - 2 * 2, Graphics.LEFT | Graphics.BASELINE);
        }
        if (isAnswer && DrawChoice.level == 1) {
            s1 = longestString(as1, as2, as3, as4);
            g.setColor(0x00000000);
            g.drawRect(draw.getWidth() - font.stringWidth(s1) - 16, draw.getHeight() - 5 * font.getHeight() - 5 * 4 - 1,
                    font.stringWidth(s1) + 10, 4 * font.getHeight() + 5 * 4);

            g.setColor(0x00F0E001);
            g.fillRect(draw.getWidth() - font.stringWidth(s1) - 15, draw.getHeight() - 5 * font.getHeight() - 5 * 4,
                    font.stringWidth(s1) + 10 - 1, 4 * font.getHeight() + 5 * 4 - 1);

            g.setColor(0x00000000);
            g.drawLine(draw.getWidth() - font.stringWidth(s1) - 16, draw.getHeight() - 4 * font.getHeight() - 4 * 4,
                    draw.getWidth() - 6, draw.getHeight() - 4 * font.getHeight() - 4 * 4);
            g.drawLine(draw.getWidth() - font.stringWidth(s1) - 16, draw.getHeight() - 3 * font.getHeight() - 3 * 4 + 1,
                    draw.getWidth() - 6, draw.getHeight() - 3 * font.getHeight() - 3 * 4 + 1);
            g.drawLine(draw.getWidth() - font.stringWidth(s1) - 16, draw.getHeight() - 2 * font.getHeight() - 2 * 4 + 2,
                    draw.getWidth() - 6, draw.getHeight() - 2 * font.getHeight() - 2 * 4 + 2);

            if (aline == 1) {
                g.setColor(0x0041F001);
                g.fillRect(draw.getWidth() - font.stringWidth(s1) - 15, draw.getHeight() - 5 * font.getHeight() - 5 * 4,
                        font.stringWidth(s1) + 10 - 1, font.getHeight() + 4);
            }
            if (aline == 2) {
                g.setColor(0x0041F001);
                g.fillRect(draw.getWidth() - font.stringWidth(s1) - 15, draw.getHeight() - 4 * font.getHeight() - 4 * 4 + 1,
                        font.stringWidth(s1) + 10 - 1, font.getHeight() + 4);
            }
            if (aline == 3) {
                g.setColor(0x0041F001);
                g.fillRect(draw.getWidth() - font.stringWidth(s1) - 15, draw.getHeight() - 3 * font.getHeight() - 3 * 4 + 2,
                        font.stringWidth(s1) + 10 - 1, font.getHeight() + 4);
            }
            if (aline == 4) {
                g.setColor(0x0041F001);
                g.fillRect(draw.getWidth() - font.stringWidth(s1) - 15, draw.getHeight() - 2 * font.getHeight() - 2 * 4 + 3,
                        font.stringWidth(s1) + 10 - 1, font.getHeight() + 4);
            }

            g.setColor(0x00000000);
            int x = draw.getWidth() - font.stringWidth(s1) - 10;
            if (x < 0) {
                x = 5;
            }
            g.drawString(as4, x,
                    draw.getHeight() - font.getHeight() - 2 - 3, Graphics.LEFT | Graphics.BASELINE);
            g.drawString(as3, x,
                    draw.getHeight() - 2 * font.getHeight() - 2 * 4 + 2 - 3, Graphics.LEFT | Graphics.BASELINE);
            g.drawString(as2, x,
                    draw.getHeight() - 3 * font.getHeight() - 3 * 4 + 1 - 3, Graphics.LEFT | Graphics.BASELINE);
            g.drawString(as1, x,
                    draw.getHeight() - 4 * font.getHeight() - 4 * 4 - 3, Graphics.LEFT | Graphics.BASELINE);

        }
    }

    public int getAnswer() {
        String ans = (String) imageInfo.getVresultSign(i);

        if (ans.equalsIgnoreCase(as1)) {
            return 1;
        } else if (ans.equalsIgnoreCase(as2)) {
            return 2;
        } else if (ans.equalsIgnoreCase(as3)) {
            return 3;
        }
        return 4;
    }

    public String getResultText() {
        return imageInfo.getVresultText(i);
    }

    public String getResultSign() {
        return imageInfo.getVresultSign(i);
    }

    public String getMean() {
        return imageInfo.getVmean(i);
    }
}
